-- FUNCTION: public.widget_FollowUp_Appointments_Count(date, integer, integer)

-- DROP FUNCTION IF EXISTS public."widget_FollowUp_Appointments_Count"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_FollowUp_Appointments_Count"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(*) from "Appointment" 
where "Active" is true and "AppointmentDate"::date="fromDate" and 
	"AppointmentTypeId" =4
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end
and case when "locationId" is null then 1=1 else "LocationId"= "locationId" end;

end

$BODY$;

ALTER FUNCTION public."widget_FollowUp_Appointments_Count"(date, integer, integer)
    OWNER TO postgres;